import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/FileUtil.dart';
import '../../../bloc/FileBrowseCubit.dart';
import '../../../sub/FileBrowsePage.dart';

class FileItemView extends StatelessWidget {
  final ImFile file;
  final FileViewType fileViewType;

  const FileItemView({required this.file, required this.fileViewType, super.key});

  @override
  Widget build(BuildContext context) {
    var fileBrowseCubit = context.read<FileBrowseCubit>();
    return Padding(padding: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10), child: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        buildRadio(fileBrowseCubit),
        const SizedBox(width: 5,),
        Image(image: AssetImage(FileUtil.getFileImagePath(file.path)), width: 35, fit: BoxFit.contain,),
        const SizedBox(width: 5,),
        Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ConstrainedBox(
              constraints: const BoxConstraints(maxWidth: 300),
              child: Text(file.fileName, style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE), softWrap: true,),
            ),
            const SizedBox(height: 5,),
            Text(FileUtil.formatFileSize(file.fileSize),
              style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE, color: Colors.grey),)
          ],
        ),
      ],
    ),);
  }

  Widget buildRadio(FileBrowseCubit fileBrowseCubit) {
    if (fileViewType == FileViewType.file) {
      return Checkbox(value: fileBrowseCubit.state.selectedFiles.contains(file),
          shape: const CircleBorder(),
          side: const BorderSide(color: Colors.black26),
          onChanged: (value){
            if (fileBrowseCubit.state.selectedFiles.contains(file)) {
              fileBrowseCubit.add(FileBrowseRemoveFileEvent(file));
            } else {
              fileBrowseCubit.add(FileBrowseAddFileEvent(file));
            }
          }
      );
    } else {
      return Checkbox(value: fileBrowseCubit.state.selectedCollectFiles.contains(file),
          shape: const CircleBorder(),
          side: const BorderSide(color: Colors.black26),
          onChanged: (value){
            if (fileBrowseCubit.state.selectedCollectFiles.contains(file)) {
              fileBrowseCubit.add(FileBrowseRemoveCollectFileEvent(file));
            } else {
              fileBrowseCubit.add(FileBrowseAddCollectFileEvent(file));
            }
          }
      );
    }
  }
}
